<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/30
 * Time: 8:36
 */
namespace Member\Controller;
use Org\Yike\Member;
class ExamineController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->is_login();
        $this->status = getStatus('examine');
    }
    public function index (){
        $model = M ('user');
        $this->status = getStatus('user');
        $field = 'u.*,info.true_name,info.reg_time,info.reg_ip,acc.blance,acc.point,acc.freeze,cate.name as cname';
        $table = array ('yk_user'=>'u','yk_user_info'=>'info','yk_user_account'=>'acc','yk_user_cate'=>'cate');
        //所有
        $a_count = $model->field ('id')->count ();
        $a_pager = new \Org\Util\Page($a_count, 15);
        $all['count'] = $a_count;
        $all['page'] = $a_pager->show();
        $all['list'] = $model->field ($field)
                        ->table ($table)
                        ->where (array ('info.user_id=u.id','acc.user_id=u.id','info.cate_id=cate.id'))
                        ->order ('info.reg_time asc')
                        ->limit ($a_pager->firstRow.','.$a_pager->listRows)
                        ->select ();
        //待审核
        $c_count = $model->field ('id')->where ('status=0')->count ();
        $c_pager = new \Org\Util\Page($c_count, 15);
        $check['count'] = $c_count;
        $check['page'] = $c_pager->show();
        $check['list'] = $model->field ($field)
                        ->table ($table)
                        ->where (array ('u.status=0','info.user_id=u.id','acc.user_id=u.id','info.cate_id=cate.id'))
                        ->order ('info.reg_time asc')
                        ->limit ($c_pager->firstRow.','.$c_pager->listRows)
                        ->select ();

        $this->assign ('all_user', $all);
        $this->assign ('check_user', $check);
        $this->display ();
    }
    public function edit (){
        if (IS_POST){
            $data = I ('post.data', '');
            $info = I ('post.info', '');
            if ($info['group'] == 0){
                $this->error ('分组不存在');
            }
            $account = I ('post.account', '');
            $d_res = M ('user')->save ($data);
            $i_res = M ('user_info')->where ('user_id='.$data['id'])->save ($info);
            $a_res = M ('user_account')->where ('user_id='.$data['id'])->save ($account);
            $d_res = $d_res===0 ? true: $d_res;
            $i_res = $i_res===0 ? true: $i_res;
            $a_res = $a_res===0 ? true: $a_res;
            if ( $d_res && $i_res && $a_res){
                //把用户写入分组
                if (!empty ($info['group'])){
                    $orid = I ('post.original','');
                    $users = trim (M ('division')->where ('id='.$info['group'])->getField('users'), ',');
                    //删除原先所在的分组
                    $orig = M ('division')->where ('id='.$orid)->getField('users');
                    $rep_str = str_replace(','.$data['id'],"",$orig);
                    M ('division')->where ('id='.$orid)->setField('users', $rep_str);

                    $arr = explode (',', $users);
                    if (!in_array ($data['id'], $arr)){
                        $ids = $users.','.$data['id'].',';
                        M ('division')->where ('id='.$info['group'])->setField('users', $ids);
                    }
                }
                $this->success('编辑成功', U('Examine/index'));
            }else{
                $this->error('编辑失败');
            }
        }
        $id = I ('get.id', '');
        $model = M ('user_info');
        $field = 'u.*,info.true_name,info.cate_id,info.address,info.group,info.id_type,info.id_code,info.sex,info.head_ico,acc.blance,acc.point,acc.freeze,cate.name as cname';
        $table = array ('yk_user'=>'u','yk_user_info'=>'info','yk_user_account'=>'acc','yk_user_cate'=>'cate');
        $info = $model->field ($field)
            ->table ($table)
            ->where (array ('u.id'=>$id,'info.user_id=u.id','acc.user_id=u.id','info.cate_id=cate.id'))
            ->find ();
        //分组名称
        $group = array ();
        if (!empty ($info['group'])){
            $group = M ('division')->field ('id,title')->where ('id='.$info['group'])->find ();
        }
        $this->assign ('group', $group);
        $this->assign ('info', $info);
        $this->display ();
    }
    public function del (){
        $id = I ('get.id', '');
        $d_res = M ('user')->delete($id);
        $i_res = M ('user_info')->where ('user_id='.$id)->delete() ;
        $a_res = M ('user_account')->where ('user_id='.$id)->delete();

        if ($d_res && $i_res && $a_res){
            $this->success('删除成功', U('Examine/index'));
        }else{
            $this->error('删除失败');
        }
    }

//房源审核
    public function used (){
        $this->status = getStatus('used');
        $member = new Member();
        $map['_string'] = 'u.status<>4 and u.status<>0';
        $a_list = $member::newgetUsedList(15,$map,true);
        $map = array (
            'u.status'=>0,
        );
        $c_list = $member::newgetUsedList(15,$map,true);

        $this->assign ('a_list', $a_list);
        $this->assign ('c_list', $c_list);

        $this->display ();
    }
    public function detail (){
        $id = I ('get.id');
        $info = M('used')->where('id='.$id.' and status<>4')->find();
        if(!$info){
            $this->error('找不到对应的房源信息');
        }
        //字典信息
        $dict = M ('room as r')->field ('r.*,b.bid,b.alias as balias,u.unid,u.alias as ualias')
            ->join ('yk_unit u on r.unit=u.unid','LEFT')
            ->join ('yk_build b on r.build=b.bid', 'LEFT')
            ->where ('rid='.$info['dict_id'])
            ->find ();
        //单元总数
        $unit_count = M ('unit')->where ('build='.$dict['bid'])->count ();
        $files = json_decode($info['file']);
        $mating = explode(',', $info['mating']);
        $info['mating'] = $mating;
        $info['files'] = $files;
        //录入人所在分组人员
        $groups = M ('user_info')->field ('g.users')
            ->table (array ('yk_user_info'=>'u'))
            ->join('yk_division g on u.group=g.id','LEFT')
            ->where ('u.user_id='.$this->user['id'])
            ->find ();
        $users = M ('User')->field ('id,username')->where (array ('id'=>array ('in',trim($groups['users'],','))))-> select ();
        //维护人
        $maintain = M ('user')->where ('id='.$info['maintain'])->getField('username');
        foreach ($users as &$val){
            if ($info['keyer'] == $val['id']){
                $val['key_sele'] = 'selected';
            }
            if ($info['maintain'] == $val['id']){
                $val['ma_sele'] = 'selected';
            }
        }
        //跟进信息
        $follows = M ('follow')->where (array ('source_id'=>$id))->order ('add_time desc')->select ();

        $this->assign ('follows', $follows);
        $this->assign ('maintain', $maintain);
        $this->assign ('users', $users);
        $this->assign ('unit_count', $unit_count);
        $this->assign('info',$info);
        $this->assign('dict',$dict);
        $this->seoKey(array('seo_title'=>'编辑房源'));
        $this->display();

    }
//客源审核
    public function cust (){

        $this->display ();
    }
}